﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;
using System.Data;
using FCNS.Data;

namespace ReportS
{
    /// <summary>
    /// CkChuRuKu.xaml 的交互逻辑
    /// </summary>
    public partial class CkChuRuKu : Window
    {
        public CkChuRuKu()
        {
            InitializeComponent();

            InitVar();
            InitEvent();
        }

        private void InitVar()
        {
            checkComboBoxStoreS.DisplayMemberPath = "Name";
            checkComboBoxStoreS.ValueMemberPath = "Id";
            checkComboBoxStoreS.ItemsSource = MainWindow.allStoreS;

            checkComboBoxGroupS.DisplayMemberPath = "Name";
            checkComboBoxGroupS.ValueMemberPath = "Id";
            foreach (DataRow row in SQLdata.GetDataRows("select Id,Name from GroupS where Flag=" + (int)Table.GroupS.EnumFlag.商品分类))
                checkComboBoxGroupS.Items.Add(new IdAndName()
                {
                    Id = (Int64)row["Id"],
                    Name = row["Name"] as string
                });
        }

        private void InitEvent()
        {
            buttonSearch.Click += (ss, ee) =>
            {
                this.Cursor = Cursors.Wait;
                FillData();
                this.Cursor = Cursors.Arrow;
            };
        }

        private void FillData()
        {
            StringBuilder sb = new StringBuilder("SELECT  StoreS.Name AS StoreSName, ProductS.Name AS ProductSName, ProductSIO.OrderNO, ProductSIO.ProductSId, ProductSIO.PurchasePrice, ProductSIO.WholesalePrice, ProductSIO.AveragePrice, ProductSIO.Quantity, ProductSIO.OrderDateTime, ProductSIO.StoreSId");
            sb.Append(" FROM ProductSIO INNER JOIN  ProductS ON ProductSIO.ProductSId = ProductS.Id INNER JOIN StoreS ON ProductSIO.StoreSId = StoreS.Id");
            sb.Append(" where cast(ProductSIO.OrderDateTime as varchar)>='" + uCDateTime1.StartDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
            sb.Append(" and cast(ProductSIO.OrderDateTime as varchar)<='" + uCDateTime1.EndDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
            sb.Append(" and ProductSIO.Enable=1");

            if (!string.IsNullOrEmpty(checkComboBoxGroupS.Text))
            {
                MessageBox.Show(checkComboBoxGroupS.SelectedValue);
                sb.Append(" and ProductSIO.ProductSId in (select Id from ProductS where ");
                foreach (string str in checkComboBoxGroupS.SelectedValue.Split(','))
                    sb.Append("GroupSId=" + str + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }
            if (!string.IsNullOrEmpty(checkComboBoxStoreS.Text))
            {
                sb.Append(" and (");
                foreach (string str in checkComboBoxStoreS.SelectedValue.Split(','))
                    sb.Append("ProductSIO.StoreSId=" + str + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }

            List<TempFunction> temp = new List<TempFunction>();
            foreach (DataRow row in SQLdata.GetDataRows(sb.ToString()))
            {
                if (row == null)
                    continue;

                TempFunction tf = new TempFunction()
                 {
                     ProductSName = row["ProductSName"] as string,
                     PurchasePrice = (double)row["PurchasePrice"],
                     WholesalePrice = (double)row["WholesalePrice"],
                     Quantity = (double)row["Quantity"],
                     AveragePrice = (double)row["AveragePrice"],
                     OrderDateTime = DateTime.ParseExact(row["OrderDateTime"] as string, DbDefine.dateTimeFormat, null),
                     StoreSName = row["StoreSName"] as string
                 };

                string orderNO = row["OrderNO"] as string;
                if (orderNO.StartsWith("CG"))
                {
                    if (orderNO.Contains("DD"))
                        tf.OrderNO = "采购订单-" + orderNO;
                    else if (orderNO.Contains("TH"))
                        tf.OrderNO = "采购退货-" + orderNO;
                    else if (orderNO.Contains("ZP"))
                        tf.OrderNO = "采购赠品-" + orderNO;
                }
                else if (orderNO.StartsWith("PF"))
                {
                    if (orderNO.Contains("DD"))
                        tf.OrderNO = "批发订单-" + orderNO;
                    else if (orderNO.Contains("TH"))
                        tf.OrderNO = "批发退货-" + orderNO;
                    else if (orderNO.Contains("ZP"))
                        tf.OrderNO = "批发赠品-" + orderNO;
                }
                else if (orderNO.StartsWith("CK")&&orderNO.Contains("PD"))
                {
                    if (tf.Quantity > 0)
                        tf.OrderNO = "盘盈-" + orderNO;
                    else
                        tf.OrderNO = "盘亏-" + orderNO;
                }
                else if (orderNO.StartsWith("POS"))
                    tf.OrderNO = "零售单-" + orderNO;

                temp.Add(tf);
            }
            dataGridExt1.ItemsSource = temp;
        }


        internal class TempFunction
        {
            public string OrderNO { get; set; }
            public string ProductSName { get; set; }
            public double PurchasePrice { get; set; }
            public double WholesalePrice { get; set; }
            public double Quantity { get; set; }
            public double AveragePrice { get; set; }
            public DateTime OrderDateTime { get; set; }
            public string StoreSName { get; set; }
        }
    }
}